<template>
    <el-card>
        <el-button type="primary" icon="Plus" @click="addTrademark">
            添加品牌
        </el-button>
        <el-table :data="trademarkArr">
            <el-table-column prop="id" label="序号" width="80px" />
            <el-table-column prop="tmName" label="品牌名称" />
            <el-table-column prop="logoUrl" label="品牌LOGO">
                <template #default="scope">
                    <div style="display: flex; align-items: center">
                        <img
                            :src="scope.row.logoUrl"
                            alt=""
                            style="width: 150px; height: 150px"
                        />
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template #default="scope">
                    <el-button
                        type="primary"
                        size="small"
                        icon="Edit"
                        @click="modifyTrademark"
                    />
                    <el-button type="primary" size="small" icon="Delete" />
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            v-model:current-page="curPage"
            v-model:page-size="limit"
            :page-sizes="[3, 5, 10, 15]"
            :background="true"
            layout="prev, pager, next, jumper, ->, sizes, total"
            :total="total"
            @current-change="changePage"
            @size-change="changePage"
        />

        <el-dialog
            v-model="dialogFormVisible"
            :title="trademarkParams.id ? '修改品牌' : '添加品牌'"
        >
            <el-form>
                <el-form-item label="品牌名称"></el-form-item>
                <el-form-item label="品牌LOGO">
                    <el-upload class="avatar-uploader">
                        <img class="avatar" />
                        <el-icon class="avatar-uploader-icon">
                            <Plus />
                        </el-icon>
                    </el-upload>
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button type="primary" size="default" @click="cancel">
                    取消
                </el-button>
                <el-button type="primary" size="default" @click="confirm">
                    确定
                </el-button>
            </template>
        </el-dialog>
    </el-card>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue"
import { reqHasTradeMark } from "@/api/product/trademark"
import type { Records, TradeMark } from "@/api/product/trademark/type"
const curPage = ref(1)
const limit = ref(3)
const total = ref(0)
const trademarkArr = ref<Records>([])
const trademarkParams = reactive<TradeMark>({
    tmName: "",
    logoUrl: "",
})
const getHasTradeMark = async () => {
    let result = await reqHasTradeMark(curPage.value, limit.value)
    if (result.code == 200) {
        total.value = result.data.total
        trademarkArr.value = result.data.records.map(fixLogoUrl)
    }
}

// 修复服务端错误的链接
const fixLogoUrl = (trademark: TradeMark) => {
    if (
        !trademark.logoUrl.startsWith("http://") &&
        !trademark.logoUrl.startsWith("https://")
    ) {
        trademark.logoUrl = "http://" + trademark.logoUrl
    }
    return trademark
}

const changePage = () => getHasTradeMark()

// 添加品牌事件
const addTrademark = () => (dialogFormVisible.value = true)
const modifyTrademark = () => (dialogFormVisible.value = true)
// 对话框取消按钮
const cancel = () => (dialogFormVisible.value = false)
// 对话框确定按钮
const confirm = () => (dialogFormVisible.value = false)
onMounted(() => {
    getHasTradeMark()
})

const dialogFormVisible = ref(false)
const dialogVisible = () => {
    dialogFormVisible.value = true
}
</script>

<style scoped lang="scss">
.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>
